package com.styletransfer.mapper;

import com.styletransfer.model.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 评论数据库映射接口
 * 
 * 提供评论相关的数据库操作方法
 */
@Mapper
public interface CommentMapper {
    
    /**
     * 插入评论
     * 
     * @param comment 评论对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO comment (work_id, user_id, content, create_time, update_time, status, type) " +
            "VALUES (#{workId}, #{userId}, #{content}, #{createTime}, #{updateTime}, #{status}, #{type})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Comment comment);
    
    /**
     * 更新评论
     * 
     * @param comment 评论对象
     * @return 影响的行数
     */
    @Update("UPDATE comment SET content = #{content}, update_time = #{updateTime} " +
            "WHERE id = #{id}")
    int update(Comment comment);
    
    /**
     * 更新评论状态
     * 
     * @param id 评论ID
     * @param status 状态（1:正常, 0:删除）
     * @return 影响的行数
     */
    @Update("UPDATE comment SET status = #{status} WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") Integer status);
    
    /**
     * 根据ID查询评论
     * 
     * @param id 评论ID
     * @return 评论对象
     */
    @Select("SELECT c.*, u.username, u.nickname, u.avatar " +
            "FROM comment c " +
            "LEFT JOIN user u ON c.user_id = u.id " +
            "WHERE c.id = #{id} AND c.status = 1")
    Comment findById(Long id);
    
    /**
     * 根据作品ID查询评论列表
     * 
     * @param workId 作品ID
     * @return 评论列表
     */
    @Select("SELECT c.*, u.username, u.nickname, u.avatar " +
            "FROM comment c " +
            "LEFT JOIN user u ON c.user_id = u.id " +
            "WHERE c.work_id = #{workId} AND c.status = 1 AND c.type = 'work' " +
            "ORDER BY c.create_time DESC")
    List<Comment> findByWorkId(Long workId);
    
    /**
     * 根据作品ID分页查询评论列表
     * 
     * @param workId 作品ID
     * @param offset 偏移量
     * @param limit 限制数
     * @return 评论列表
     */
    @Select("SELECT c.*, u.username, u.nickname, u.avatar " +
            "FROM comment c " +
            "LEFT JOIN user u ON c.user_id = u.id " +
            "WHERE c.work_id = #{workId} AND c.status = 1 AND c.type = 'work' " +
            "ORDER BY c.create_time DESC " +
            "LIMIT #{offset}, #{limit}")
    List<Comment> findByWorkIdWithPage(@Param("workId") Long workId, 
                                      @Param("offset") Integer offset, 
                                      @Param("limit") Integer limit);
    
    /**
     * 根据作品ID查询评论数量
     * 
     * @param workId 作品ID
     * @return 评论数量
     */
    @Select("SELECT COUNT(*) FROM comment WHERE work_id = #{workId} AND status = 1 AND type = 'work'")
    Long countByWorkId(Long workId);
    
    /**
     * 根据用户ID查询评论列表
     * 
     * @param userId 用户ID
     * @return 评论列表
     */
    @Select("SELECT c.*, u.username, u.nickname, u.avatar " +
            "FROM comment c " +
            "LEFT JOIN user u ON c.user_id = u.id " +
            "WHERE c.user_id = #{userId} AND c.status = 1 " +
            "ORDER BY c.create_time DESC")
    List<Comment> findByUserId(Long userId);
    
    /**
     * 根据目标ID和类型查询评论列表
     * 
     * @param targetId 目标ID（作品ID或专辑ID）
     * @param type 评论类型
     * @return 评论列表
     */
    @Select("SELECT c.*, u.username, u.nickname, u.avatar " +
            "FROM comment c " +
            "LEFT JOIN user u ON c.user_id = u.id " +
            "WHERE c.work_id = #{targetId} AND c.status = 1 AND c.type = #{type} " +
            "ORDER BY c.create_time DESC")
    List<Comment> findByTargetIdAndType(@Param("targetId") Long targetId, @Param("type") String type);
    
    /**
     * 根据目标ID和类型分页查询评论列表
     * 
     * @param targetId 目标ID（作品ID或专辑ID）
     * @param type 评论类型
     * @param offset 偏移量
     * @param limit 限制数
     * @return 评论列表
     */
    @Select("SELECT c.*, u.username, u.nickname, u.avatar " +
            "FROM comment c " +
            "LEFT JOIN user u ON c.user_id = u.id " +
            "WHERE c.work_id = #{targetId} AND c.status = 1 AND c.type = #{type} " +
            "ORDER BY c.create_time DESC " +
            "LIMIT #{offset}, #{limit}")
    List<Comment> findByTargetIdAndTypeWithPage(
            @Param("targetId") Long targetId, 
            @Param("type") String type,
            @Param("offset") Integer offset, 
            @Param("limit") Integer limit);
    
    /**
     * 根据目标ID和类型查询评论数量
     * 
     * @param targetId 目标ID（作品ID或专辑ID）
     * @param type 评论类型
     * @return 评论数量
     */
    @Select("SELECT COUNT(*) FROM comment WHERE work_id = #{targetId} AND status = 1 AND type = #{type}")
    Long countByTargetIdAndType(@Param("targetId") Long targetId, @Param("type") String type);
}
